home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / src / plugin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-16  |  3.6 KB  |  133 lines

  1. /* plugin.c- plugin
  2.  *
  3.  *  Window Maker window manager
  4.  *
  5.  *  Copyright (c) hmmm... Should I put everybody's name here?
  6.  *  Where's my lawyer?? -- ]d :D
  7.  *
  8.  *  This program is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2 of the License, or
  11.  *  (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  21.  *  USA.
  22.  * 
  23.  * * * * * * * * *
  24.  * Do you think I should move this code into another file? -- ]d
  25.  */
  26.  
  27. #include "plugin.h"
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32.  
  33. #ifdef TEXTURE_PLUGIN
  34. # ifdef HAVE_DLFCN_H
  35. #  include <dlfcn.h>
  36. # endif
  37. #endif
  38.  
  39. #include <proplist.h>
  40.  
  41.  
  42. void** 
  43. wPluginPackInitData(int members, ...) 
  44. {
  45.     void **p;
  46.     va_list vp;
  47.     int i;
  48.     p = wmalloc(sizeof(void *) * (members + 1));
  49.     memset(p, 0, sizeof(void *) * (members + 1));
  50.     va_start(vp, members);
  51.     for(i=0;i<members;i++) {
  52.         p[i] = va_arg(vp, void *);
  53.         printf(" %d > %d\n",i,(int)p[i]);
  54.     }
  55.     printf(" s> %s\n",(char*)p[2]);
  56.     va_end(vp);
  57.     return p;
  58. }
  59.  
  60. WFunction *
  61. wPluginCreateFunction(int type, char *library_name,
  62.         char *init_proc_name, char *proc_name, char *free_data_proc_name,
  63.         proplist_t pl_arg, void *init_data) 
  64. {
  65.     WFunction *function;
  66.     _DL_InitDataProc *initProc;
  67.  
  68.     function = wmalloc(sizeof(WFunction));
  69.     memset(function, 0, sizeof(WFunction));
  70.  
  71.     function->handle = dlopen(library_name, RTLD_LAZY);
  72.     if (!function->handle) {
  73.         wwarning(_("library \"%s\" cound not be opened."), library_name);
  74.         free(function);
  75.         return NULL;
  76.     }
  77.  
  78.     function->proc.any = dlsym(function->handle, proc_name);
  79.     if (!function->proc.any) {
  80.         wwarning(_("function \"%s\" not found in library \"%s\""), proc_name, library_name);
  81.         dlclose(function->handle);
  82.         free(function);
  83.         return NULL;
  84.     }
  85.  
  86.     if (free_data_proc_name) {
  87.         function->freeData = dlsym(function->handle, free_data_proc_name);
  88.         if (!function->freeData) {
  89.             wwarning(_("function \"%s\" not found in library \"%s\""), free_data_proc_name, library_name);
  90.             /*
  91.             dlclose(function->handle);
  92.             free(function);
  93.             return NULL;
  94.             */
  95.         }
  96.     }
  97.  
  98.     if (pl_arg) function->arg = PLDeepCopy(pl_arg);
  99.     function->data = init_data;
  100.     if (init_proc_name) {
  101.         initProc = dlsym(function->handle, init_proc_name);
  102.         if (initProc) {
  103.             initProc(function->arg, function->data);
  104.         } else {
  105.             /* Where's my english teacher? -- ]d
  106.             wwarning(_("ignore?"),?);
  107.             */
  108.         }
  109.     }
  110.  
  111.     function->type = type;
  112.     return function;
  113. }
  114.  
  115. void
  116. wPluginDestroyFunction(WFunction *function) 
  117. {
  118.     if (!function) 
  119.     return;
  120.  
  121.     if (function->data) {
  122.         if (function->freeData) {
  123.             function->freeData(&function->data);
  124.         } else {
  125.             free(function->data);
  126.         }
  127.     }
  128.     if (function->arg) PLRelease(function->arg);
  129.     free(function);
  130.     return;
  131. }
  132.  
  133.